import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:image_picker/image_picker.dart';
import '../../spaces/image_space/image_helper.dart';

import '../../helper/uidata.dart';

import 'package:photo_view/photo_view.dart';
import '../../helper/fileHelper.dart';

import '../../model/recording.dart';

import '../../helper/Helper.dart';
import 'record_list.dart';

class RecordAddPage extends StatefulWidget {
  @override
  RecordAddPage(this.SpaceId, this.added);
  final String SpaceId;
  final RecordAddCallback added;
  @override
  RecordAddPageState createState() => new RecordAddPageState();
}

class RecordAddPageState extends State<RecordAddPage> {
  late File _img;
  bool upLoading = false;
  final picker = ImagePicker();

  Future getImg() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);

    if (pickedFile != null) {
      setState(() {
        //_img = image2;
        _img = File(pickedFile.path);
      });
    } else {
      return;
    }
  }

  openPhoto() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);

    if (pickedFile != null) {
      setState(() {
        //_img = image2;
        _img = File(pickedFile.path);
      });
    } else {
      return;
    }
  }

  ///压缩
  compressImg() async {
    if (_img == null) return;
    if (!_img.existsSync()) return;

    var img2 = await Helper.ImgCompress(_img);
    setState(() {
      _img = img2;
    });
  }

  rotateImg() async {
    if (_img == null) return;
    if (!_img.existsSync()) return;

    var Img2 = await Helper.ImgRotate(_img);
    setState(() {
      _img = Img2;
    });
  }

  Future upLoadImg() async {
    if (_img == null) return;

    setState(() {
      upLoading = true;
    });
    //var image2 = await Helper.ImgCompress(_img);
    var guid = await ImageSpaceHelper.UpLoad(_img);
    // AMapLocation p1;

    //  p1 = await AMapLocationClient.getLocation(true);

    var x1 = new Recording(
        0,
        widget.SpaceId,
        //UIData.CurrentUser!.Guid,
        UIData.CurrentUser!.Guid,
        "",
        43,
        123,
        "",
        1,
        guid,
        Helper.DateTimeNowString(),
        "123",
        "");

    var pb1 = await Recording.Add(x1);

    if (mounted) {
      setState(() {
        upLoading = false;
      });
    }

    Helper.fileCache.putFile(Helper.GetImgUrl(guid), _img.readAsBytesSync());

    if (pb1 == true) {
      Navigator.of(context).pop();
      widget.added(x1);
    }
  }

  @override
  initState() {
    super.initState();
    getImg();
  }

  @override
  void dispose() {
    //注意这里关闭
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    if (_img == null) return new Container();

    return Scaffold(
      appBar: AppBar(
        title: Text("上传图片"),
      ),
      persistentFooterButtons: <Widget>[
        Text(
          Helper.GetFileSize(_img.lengthSync()),
          style: TextStyle(color: Colors.black38),
        ),
        ElevatedButton(
          child: Text("压缩"),
          onPressed: () {
            compressImg();
          },
        ),
        ElevatedButton(
          child: Text("旋转"),
          onPressed: () {
            rotateImg();
          },
        ),
        ElevatedButton(
          child: Text("相册"),
          onPressed: () {
            openPhoto();
          },
        ),
        ElevatedButton(
          // color: Colors.blue,
          //  textColor: Colors.white,
          child: Text("上传"),
          onPressed: () {
            upLoadImg();
          },
        )
      ],
      body: upLoading == true
          ? Center(
              child: SpinKitCircle(
              color: Colors.blue,
            ))
          : Container(
              // ignore: unnecessary_null_comparison
              child: _img == null
                  ? Center(child: Text("未选择图片"))
                  : PhotoView(
                      imageProvider: FileImage(_img),
                      //  backgroundColor: GlobalConfig.cardBackgroundColor,
                    ),
            ),
    );
  }
}
